package d.n.d.dao;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Repository;
import d.n.d.domain.Account;

/**
 * @author D.N.D
 */
@Repository
public class AccountDaoImpl implements AccountDao {

	@PersistenceContext
	private EntityManager em;

	/**
	 * <br> Find account information by account information number.<br/>
	 */
	public Account findAccountInfoByAccountInfoNumber(String accountNumber) {

		TypedQuery<Account> query = em
				.createQuery(
						"SELECT acc FROM Account acc WHERE acc.accountNumber =:accountNumber",
						Account.class).setParameter("accountNumber",
						accountNumber);

		List<Account> list = query.getResultList();
		if (list.size() == 0)
			return null;
		return list.get(0);
	}

	/**
	 * <br> Get list account by id card active. <br/>
	 */
	public List<Account> getListAccountByIdCardActive(String idCardNumber) {

		TypedQuery<Account> query = em
				.createQuery(
						"SELECT acc FROM Account acc WHERE acc.idCardNumber.idCardNumber =:idCardNumber  and acc.accountState = 'active'",
						Account.class);
		return query.setParameter("idCardNumber", idCardNumber).getResultList();
	}

	/**
	 * <br> Get account list by id card number. <br/>
	 */
	public List<Account> getAccountListByIdCardNumber(String idCardNumber) {

		TypedQuery<Account> query = em
				.createQuery(
						"SELECT acc FROM Account acc WHERE acc.idCardNumber.idCardNumber =:idCardNumber",
						Account.class);
		return query.setParameter("idCardNumber", idCardNumber).getResultList();
	}
}
